Ext.define('wmf.controller.channel.channel', {
    extend: 'Ext.app.Controller',
    stores: ['channel.channel',
             'channel.channelType',
             'channel.billingType',
             'channel.needSharing',
             'channel.carrier',
             'channel.classUsage1',
             'channel.classUsage2',
             'channel.classUsage3',
             'channel.classUsage'
    ],
    models: ['channel.channel',
             'channel.classUsage'
    ],
    views: ['channel.main', 
            'channel.list', 
            'channel.east', 
            'channel.add', 
            'channel.edit', 
            'channel.query'
    ],
    
    refs: [{
        ref: 'channelList',
        selector: 'channelList'
    },{
        ref: 'channelMain',
        selector: 'channelMain'
    },{
        ref: 'channelQuery',
        selector: 'channelQuery'
    },{
        ref: 'channelAdd',
        selector: 'channelAdd'
    },{
        ref: 'channelEdit',
        selector: 'channelEdit'
	},{
        ref: 'channelEast',
        selector: 'channelEast'
    }],
    
    init: function () {
        this.control({
        	'channelList': {
        		beforerender : this.onBeforeRender,
        		celldblclick: this.onCellDblClick
            },
            'channelList button#add': {
                click: this.onAddButtonClick
            },
            'channelList button#query': {
                click: this.onQueryButtonClick
            },
            'channelList button#export': {
                click: this.onExportButtonClick
            },
			'channelEast tool#minimize': {
				click: this.onMinToolClick
			},
			'channelAdd button#save': {
				click: this.onAddSaveButtonClick
			},
			'channelAdd button#reset': {
				click: this.onAddResetButtonClick
			},
			'channelAdd button#cancel': {
				click: this.onMinToolClick
			},
			'channelEdit button#save': {
				click: this.onEditSaveButtonClick
			},
			'channelEdit button#reset': {
				click: this.onEditResetButtonClick
			},
			'channelEdit button#cancel': {
				click: this.onMinToolClick
			},
        	'channelQuery button#save': {
				click: this.onQuerySaveButtonClick
			},
			'channelQuery button#reset': {
				click: this.onQueryResetButtonClick
			},
			'channelQuery button#cancel': {
				click: this.onMinToolClick
			}
        });
        wmf.channel = {};
        wmf.channel.params = {};
        wmf.channel.data = {};
        wmf.channel.init = true;
    },

    onAddButtonClick: function(){
    	var win = this.getChannelEast();
        win.removeAll();
        win.add(Ext.widget("channelAdd"));
        win.show();
    },
    
    onQueryButtonClick: function(){
    	var win = this.getChannelEast();
        win.removeAll();
        win.add(Ext.widget("channelQuery"));
        win.show();
        this.getChannelQuery().getForm().setValues(wmf.channel.params);
    },
    
    onExportButtonClick: function(){
    	var shopId = wmf.channel.params.shopId ? wmf.channel.params.shopId : '';
		var startTime = wmf.channel.params.startTime ? wmf.channel.params.startTime : '';
		var endTime = wmf.channel.params.endTime ? wmf.channel.params.endTime : '';
		window.location.href='channel/exportchannel.do?shopId='
			+encodeURI(shopId)+'&startTime='
			+encodeURI(startTime)+'&endTime='
			+encodeURI(endTime);
    },
    
    onCellDblClick: function(list, td, cellIndex, record, tr, rowIndex, e, eOpts){
    	var win = this.getChannelEast();
        win.removeAll();
        win.add(Ext.widget("channelEdit"));
        win.show();
        wmf.channel.data = record.data;
        this.getChannelEdit().loadRecord(record);
    },
    
	onAddSaveButtonClick: function(){
    	var me = this;
    	var former = this.getChannelAdd();
    	var value = former.getValues();
		var win = this.getChannelEast();
		Ext.Ajax.request({
			url:'channel/saveChannel.do',
			params: value,
			success : function(respond, opts) {
				var responds = Ext.decode(respond.responseText);
				if(responds.failure){
					Ext.Msg.alert("提示", responds.msg);
					return false;
				}
				win.hide();
				me.getChannelList().getStore().reload();
			},
			failure : function(respond, action) {
				Ext.Msg.alert("提示", action.result.msg ? action.result.msg : "网络错误");
			}
		});
    },

    onAddResetButtonClick: function(){
    	this.getChannelAdd().getForm().reset();
    },
    
    onEditSaveButtonClick: function(){
    	var me = this;
    	var former = this.getChannelEdit();
    	var value = former.getValues();
		var win = this.getChannelEast();
		Ext.Ajax.request({
			url:'channel/saveChannel.do',
			params: value,
			success : function(respond, opts) {
				var responds = Ext.decode(respond.responseText);
				if(responds.failure){
					Ext.Msg.alert("提示", responds.msg);
					return false;
				}
				win.hide();
				me.getChannelList().getStore().reload();
			},
			failure : function(respond, action) {
				Ext.Msg.alert("提示", action.result.msg ? action.result.msg : "网络错误");
			}
		});
    },

    onEditResetButtonClick: function(){
    	this.getChannelEdit().getForm().setValues(wmf.channel.data);
    },
    
    onQueryResetButtonClick: function(){
    	var form = this.getChannelQuery().getForm();
    	form.reset();
    },
    
	onQuerySaveButtonClick: function(){
    	var me = this;
    	var former = this.getChannelQuery();
    	var value = former.getValues();
		var win = this.getChannelEast();
		wmf.channel.init = true;
		wmf.channel.params.channelName = value.channelName;
		wmf.channel.params.channelType = value.channelType;
		win.hide();
		me.getChannelList().getStore().pagebar.getStore().currentPage = 1;
		me.getChannelList().getStore().reload();
    },
	
	onMinToolClick: function(){
    	var win = this.getChannelEast();
    	win.hide();
    },
    
    onBeforeRender: function(component,options){
    	wmf.data.loadAction(this.getChannelMain().itemId, this.getChannelList());
    	wmf.channel.params = {};
    	component.getStore().load();
    }
});